package com.kecheng.action;

import com.kecheng.dao.CommonDao;
import com.kecheng.moban.PageBean;
import com.kecheng.pojo.Teacher;
import com.kecheng.pojo.TeacherKeShi;
import com.kecheng.util.TimeUtil;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 后台课程表模块
 *
 * @author
 */
@Action(value = "teacherKeshi", results = {
        @Result(name = "teacherKeshiList", location = "/admin/teacherKeshi/teacherKeshiList.jsp"),
        @Result(name = "toDetail", location = "/admin/teacherKeshi/detail.jsp"),
        @Result(name = "redirectList", type = "redirect",location = "teacherKeshi!teacherKeshiList.action"),
})
@AllowedMethods(value = {("regex:.*")})
public class KeshiAction extends BaseAction {

    @Autowired
    CommonDao dao;
    private Teacher teacher;
    private List<List<TeacherKeShi>>lst;
    private List<String>lstDate;
    private PageBean<Teacher> pageBean;
    private Integer weekIndex;

    /**
     * 教师列表
     * @return
     */
    public String teacherKeshiList(){
        pageBean = (PageBean<Teacher>) dao.findPageBean(Teacher.class,pageBean);
        return "teacherKeshiList";
    }
    /**
     *教师课程表详情页
     * @return
     */
    public String toDetail(){
        if (weekIndex==null)weekIndex=0;//当前周默认为0，上一周就是-1，下一周就是1，以此类推
        teacher = (Teacher)dao.findOne(teacher);
        List<TeacherKeShi>teacherKeShis = null;
        TeacherKeShi tk = null;
        lst = new ArrayList<>(7);
        lstDate = new ArrayList<>(7);
        for(int i=2;i<9;i++){
            int a = i+weekIndex*7;
            String date = TimeUtil.getString("YYYY-MM-dd",TimeUtil.getWeekDate(a-2));//查询周一至周日对应的日期
            lstDate.add(i-2,date);
            tk = new TeacherKeShi();
            tk.setTeacherId(teacher.getId());
            tk.setDate(date);
            teacherKeShis = (List<TeacherKeShi>)dao.find(tk);
            lst.add(i-2,teacherKeShis);

        }
        return "toDetail";
    }


    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    public PageBean<Teacher> getPageBean() {
        return pageBean;
    }

    public void setPageBean(PageBean<Teacher> pageBean) {
        this.pageBean = pageBean;
    }

    public List<List<TeacherKeShi>> getLst() {
        return lst;
    }

    public void setLst(List<List<TeacherKeShi>> lst) {
        this.lst = lst;
    }

    public Integer getWeekIndex() {
        return weekIndex;
    }

    public void setWeekIndex(Integer weekIndex) {
        this.weekIndex = weekIndex;
    }

    public List<String> getLstDate() {
        return lstDate;
    }

    public void setLstDate(List<String> lstDate) {
        this.lstDate = lstDate;
    }
}
